/**************************************************************************************
 *
 * @(#) CustomAuthenticationEntryPoint.java
 * @Package com.jgdt.jgdtkasannong_university_gene_backend.common.example
 **************************************************************************************/
package com.jgdt.jgdtkasannong_university_gene_backend.common.example.global;

import cn.hutool.json.JSONObject;
import com.jgdt.jgdtkasannong_university_gene_backend.common.enums.SystemCodeEnums;
import com.jgdt.jgdtkasannong_university_gene_backend.common.response.ResultVo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * @description: 捕获Security自定义抛出异常信息
 * @author: huangAn
 * @createDate: 2025/04/18
 */
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest req, HttpServletResponse res, AuthenticationException ex) throws IOException {
        res.setContentType("application/json; charset=UTF-8");
        res.setStatus(HttpStatus.UNAUTHORIZED.value());
        ResultVo<?> resultVo = new ResultVo();
        resultVo.setCode(SystemCodeEnums.UNAUTHORIZED.getKey());
        resultVo.setMessage(SystemCodeEnums.UNAUTHORIZED.getMsg());
        resultVo.setDataResult(null);
        JSONObject json = new JSONObject(resultVo);
        res.getWriter().write(String.valueOf(json).trim());
    }
}




